package com.qf.fronted.controller;

import com.qf.admin.domain.Jpush;
import com.qf.admin.mapper.JpushMapper;
import com.qf.commons.Data;
import com.qf.fronted.service.IOrderService;
import com.qf.fronted.service.WeiXinPayService;
import com.qf.timer.MessageDelay;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("/pay")
@CrossOrigin("*")
public class WxpayController {
    private IOrderService orderServiceImpl2;
    private JpushMapper jpushMapper;

    private WeiXinPayService weiXinPayServiceImpl;

    public WxpayController(IOrderService orderServiceImpl2, JpushMapper jpushMapper, WeiXinPayService weiXinPayServiceImpl) {
        this.orderServiceImpl2 = orderServiceImpl2;
        this.jpushMapper = jpushMapper;
        this.weiXinPayServiceImpl = weiXinPayServiceImpl;
    }

// http://localhost:8080/ssm_project_war/pay/createNative/1147111/55
    // http://localhost:8080/ssm_project_war/pay/queryPayStatus/11471114
    /**
     * 生成二维码
     * @return
     */
    @RequestMapping("/createNative/{orderNo}/{totalPrice:[0-9\\.]+}")
    public Map createNative(@PathVariable("orderNo") String orderNo,@PathVariable("totalPrice") String totalPrice){
        BigDecimal totola_fee_big = new BigDecimal(totalPrice);//金额转BigDecimal
        BigDecimal nums = new BigDecimal(100);//定义倍数
        BigDecimal totola_fee_fen = totola_fee_big.multiply(nums).setScale(0);//元转分
        String totola_fee = totola_fee_fen.toString();
        System.out.println(totola_fee);
//        System.out.println(weiXinPayServiceImpl);
        Map map = weiXinPayServiceImpl.createNative(orderNo, totola_fee);
        return map;
    }

    @RequestMapping("/queryPayStatus/{orderNo}")
    public Data queryPayStatus(@PathVariable("orderNo") String orderNo){
        Data data=null;
        int x = 0;
        while(true){
            //调用查询接口
            Map<String,String> map = weiXinPayServiceImpl.queryPayStatus(orderNo);
            if(map==null){//出错
                data=new  Data(-1, "false");
                break;
            }
            if("SUCCESS".equals(map.get("trade_state"))){//如果成功
                data=new  Data(1, "success");
                // 极光推送
                Map<String, String> parms = new HashMap<>(8);
                Jpush jpush = jpushMapper.selectById(29);
                // Jpush jpush = jpushMapper.selectById(order.getId());
                parms.put("id", jpush.getRegId());
                parms.put("msg", "单号："+orderNo);
                MessageDelay.jpushAndroid(parms, "微信付款成功");
                // 微信生成流水
                orderServiceImpl2.updateOrderStatusAndCreateTag("trade_no", orderNo,"微信支付");
                break;
            }
            try {
                Thread.sleep(3000);//间隔三秒
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //为了不让循环无休止地运行，我们定义一个循环变量，如果这个变量超过了这个值则退出循环，设置时间为5分钟
            x++;
            if(x>=100){
                data=new  Data(0, "二维码超时");
                break;
            }
        }
        return data;
    }
}
